package com.koudai.reader.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.koudai.reader.vo.Book;

public class SingleDAO {

    private static SingleDAO instance = new SingleDAO();

    private DatabaseHelper dbhelper;
    private SQLiteDatabase db;
    private static String dbName = "shuqi_shucheng";
    private static int dbVersion = 1;
    private Context context;

    private static String TABLE_HISTORY_BOOK = "book_history";


    private SingleDAO() {

    }

    public static SingleDAO getinstance() {
        
        return instance;
    }

    public void insertBookshelf(Book book) {
        if (context != null) {

            ContentValues map = new ContentValues();
            map.put("bookid", book.getBookid());
            map.put("bookname", book.getTitle());
            map.put("currentchapterid", 0);
            map.put("currentposition", 0);
            map.put("lastchapter", book.getServerLastChapter());
            map.put("downloadurl", book.getDownloadPath());
            db.insert(TABLE_HISTORY_BOOK, null, map);
            
        }
    }
    
    public Book getBookshelf(int bookid){
        if (db != null) {
            Cursor cursor = db.query(TABLE_HISTORY_BOOK, new String[]{"bookid, bookname, currentchapterid, currentposition, lastchapter, downloadurl"}, "bookid=?", new String[]{String.valueOf(bookid)}, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    Book book = new Book();
                    book.setBookid(cursor.getInt(cursor.getColumnIndex("bookid")));
                    book.setTitle(cursor.getString(cursor.getColumnIndex("bookname")));
                    book.setCurrentChapter(cursor.getInt(cursor.getColumnIndex("currentchapterid")));
                    book.setCurrentposition(cursor.getInt(cursor.getColumnIndex("currentposition")));
                    book.setServerLastChapter(cursor.getString(cursor.getColumnIndex("lastchapter")));
                    book.setDownloadPath(cursor.getString(cursor.getColumnIndex("downloadurl")));
                    return book;
                }
            }
        }
        
        return null;
    }
    
    public List<Book> getBookshelfList () {
        
        List<Book> books = new ArrayList<Book>();
        if (db != null) {
            Cursor cursor = db.query(TABLE_HISTORY_BOOK, new String[]{"bookid, bookname, currentchapterid, currentposition, lastchapter, downloadurl"}, null, null, null, null, "id desc");
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    Book book = new Book();
                    book.setBookid(cursor.getInt(cursor.getColumnIndex("bookid")));
                    book.setTitle(cursor.getString(cursor.getColumnIndex("bookname")));
                    book.setCurrentChapter(cursor.getInt(cursor.getColumnIndex("currentchapterid")));
                    book.setCurrentposition(cursor.getInt(cursor.getColumnIndex("currentposition")));
                    book.setServerLastChapter(cursor.getString(cursor.getColumnIndex("lastchapter")));
                    book.setDownloadPath(cursor.getString(cursor.getColumnIndex("downloadurl")));
                    books.add(book);
                }
            }
        }
        return books;
    }    
    
    public boolean isBookExist(int bookid) {
        
        boolean flg = false;
        if (db != null) {
            Cursor cursor = db.query(TABLE_HISTORY_BOOK, new String[]{"bookid"}, "bookid=?", new String[]{String.valueOf(bookid)}, null, null, null);
            
            while (cursor.moveToNext()) {
                flg = true;
            }
        }

        return flg;
    }
    
    public void updateLastchapter(Book book) {
        if (context != null) {

            ContentValues map = new ContentValues();

            map.put("lastchapter", book.getServerLastChapter());
            
            db.update(TABLE_HISTORY_BOOK, map, "bookid = ?", new String[]{String.valueOf(book.getBookid())});
            
        }
    }
    
    public void updateReadingStatus(Book book) {
        if (context != null) {

            ContentValues map = new ContentValues();

            map.put("currentchapterid", book.getCurrentChapter());
            map.put("currentposition", book.getCurrentposition());
            
            db.update(TABLE_HISTORY_BOOK, map, "bookid = ?", new String[]{String.valueOf(book.getBookid())});
            
        }
    }
    
    public void init(Context context) {
        this.context = context;

        try {
            DBScriptCache.getInstance().init(this.context);
            if (dbhelper == null) {
                dbhelper = new DatabaseHelper(this.context, dbName, null, dbVersion);
            }

            if (db == null || !db.isOpen()) {
                db = dbhelper.getWritableDatabase();
            }
        } catch (DBException e) {

            e.printStackTrace();
        }
    }

    public void close() {
        if (db != null && db.isOpen()) {
            db.close();
        }
    }
}
